package com.lancoo.answer.util

import com.lancoo.answer.model.bean.TextInfo
import java.util.regex.Pattern

/**
 * 改错题内部数据数理工具类
 */
class CorrectMistakeUtils {
    companion object {
        /**
         * 是否包含标点
         */
        private fun isContainSpecialChar(str: String): Boolean {
            val regEx = "[\"'.,:!?;—-]"
            val p = Pattern.compile(regEx)
            val m = p.matcher(str)
            return m.find() && str.length == 1
        }

        /**
         * 是否包含中文
         */
        fun isContainChinese(str: String): Boolean {
            val regEx = "^[\u4e00-\u9fa5]*\$"
            val p = Pattern.compile(regEx)
            val m = p.matcher(str)
            return m.find()
        }

        /**
         * 数字化答案转成文字
         *
         * @param position
         * @param textInfoBeans
         * @return
         */
        fun toStr(position: Int, textInfoBeans: List<TextInfo>): String {
            return when (position) {
                0 -> toTextAnswer(1, position, textInfoBeans)
                textInfoBeans.size - 2 -> toTextAnswer(0, position, textInfoBeans)
                else -> toTextAnswer(2, position, textInfoBeans)
            }
        }

        /**
         * 过滤标点特殊符号，找出前后英文
         * @param mode 0:往前 1:往后 2：前后各找
         */
        private fun toTextAnswer(
            mode: Int,
            position: Int,
            textInfoBeans: List<TextInfo>
        ): String {
            val answerBean = textInfoBeans[position]
            val type = answerBean.stuType
            var before = ""
            var center = ""
            var after = ""
            when (mode) {
                0 -> {
                    loop1@ for (i in position - 1 downTo 0) {
                        if (textInfoBeans[i].Index != -1 && !isContainSpecialChar(textInfoBeans[i].Text)) {
                            center = "(${textInfoBeans[i].Text})"
                            for (j in i - 1 downTo 0) {
                                if (textInfoBeans[j].Index != -1 && !isContainSpecialChar(
                                        textInfoBeans[j].Text
                                    )
                                ) {
                                    before = "(${textInfoBeans[j].Text})"
                                    break@loop1
                                }
                            }
                        }
                    }
                    return when (type) {
                        0 -> "$before$center ${answerBean.Text} →删除${answerBean.Text}"
                        1 -> "$before$center ${answerBean.Text} →${answerBean.stuAnswer}"
                        2 -> "$center ∧ (${answerBean.Text}) →${answerBean.stuAnswer}"
                        else -> "$center(${answerBean.Text}) ∧ →${answerBean.stuAnswer}"
                    }
                }
                1 -> {
                    loop1@ for (i in position + 1 until textInfoBeans.size) {
                        if (textInfoBeans[i].Index != -1 && !isContainSpecialChar(textInfoBeans[i].Text)) {
                            center = "(${textInfoBeans[i].Text})"
                            for (j in i + 1 until textInfoBeans.size) {
                                if (textInfoBeans[j].Index != -1 && !isContainSpecialChar(
                                        textInfoBeans[j].Text
                                    )
                                ) {
                                    after = "(${textInfoBeans[j].Text})"
                                    break@loop1
                                }
                            }
                        }
                    }
                    return when (type) {
                        0 -> " (${answerBean.Text}) $center$after→删除${answerBean.Text}"
                        1 -> " (${answerBean.Text}) $center$after→${answerBean.stuAnswer}"
                        2 -> " ∧ (${answerBean.Text})$center→${answerBean.stuAnswer}"
                        else -> "(${answerBean.Text}) ∧ $center→${answerBean.stuAnswer}"
                    }
                }
                else -> {
                    for (i in position - 1 downTo 0) {
                        if (textInfoBeans[i].Index != -1 && !isContainSpecialChar(textInfoBeans[i].Text)) {
                            before = "(${textInfoBeans[i].Text})"
                            break
                        }
                    }
                    for (j in position + 1 until textInfoBeans.size) {
                        if (textInfoBeans[j].Index != -1 && !isContainSpecialChar(
                                textInfoBeans[j].Text
                            )
                        ) {
                            after = "(${textInfoBeans[j].Text})"
                            break
                        }
                    }
                    return when (type) {
                        0 -> "$before ${answerBean.Text} $after→删除${answerBean.Text}"
                        1 -> "$before ${answerBean.Text} $after→${answerBean.stuAnswer}"
                        2 -> "$before ∧ (${answerBean.Text})→${answerBean.stuAnswer}"
                        else -> "(${answerBean.Text}) ∧ $after→${answerBean.stuAnswer}"
                    }
                }
            }
        }

        /**
         * 拼接答案
         *
         * @param answerBean
         * @param answerStr
         * @return
         */
        fun toArr(answerBean: TextInfo, answerStr: String): String {
            val numStr = if (answerBean.Type == 0) {
                answerBean.Index.toString() + "*|" + answerBean.stuType + "*|"
            } else {
                answerBean.Index.toString() + "*|" + answerBean.stuType + "*|" + answerBean.stuAnswer
            }
            return "$numStr@|$answerStr"
        }
    }
}